home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / tvtoys04.zip / TVUTILS.PAS < prev    next >
Pascal/Delphi Source File  |  1993-12-18  |  13KB  |  443 lines

  1. (***************************************************************************
  2.   TVUtils unit
  3.   Validators, odd utilities, TV stuff
  4.   PJB December 14, 1993, Internet mail to d91-pbr@nada.kth.se
  5.   Copyright PJB 1993, All Rights Reserved.
  6.   Free source, use at your own risk.
  7.   If modified, please state so if you pass this around.
  8.  
  9. ***************************************************************************)
  10. unit TVUtils;
  11. {$X+}
  12.  
  13. interface
  14.  
  15.   uses
  16.     Dos,
  17.     App, Dialogs, Menus, MsgBox, Objects, Validate, Views,
  18.     toyPrefs, {$I hcFile}
  19.     toyUtils;
  20.  
  21.   const
  22.     kbCtrlA = $1E01;
  23.     kbCtrlB = $3002;
  24.     kbCtrlC = $2E03;
  25.     kbCtrlD = $2004;
  26.     kbCtrlE = $1205;
  27.     kbCtrlF = $2106;
  28.     kbCtrlG = $2207;
  29.     kbCtrlH = $2308;
  30.     kbCtrlI = $1709;
  31.     kbCtrlJ = $240A;
  32.     kbCtrlK = $250B;
  33.     kbCtrlL = $260C;
  34.     kbCtrlM = $320D;
  35.     kbCtrlN = $310E;
  36.     kbCtrlO = $180F;
  37.     kbCtrlP = $1910;
  38.     kbCtrlQ = $1011;
  39.     kbCtrlR = $1312;
  40.     kbCtrlS = $1F13;
  41.     kbCtrlT = $1414;
  42.     kbCtrlU = $1615;
  43.     kbCtrlV = $2F16;
  44.     kbCtrlW = $1117;
  45.     kbCtrlX = $2D18;
  46.     kbCtrlY = $1519;
  47.     kbCtrlZ = $2C1A;
  48.  
  49.   type
  50.     PByte      = ^Byte;
  51.     PWord      = ^Word;
  52.     PByteArray = ^TByteArray;
  53.     PWordArray = ^TWordArray;
  54.  
  55.  
  56.     (* Validate a path *)
  57.     PPathValidator = ^TPathValidator;
  58.     TPathValidator =
  59.       object (TValidator)
  60.         procedure Error; virtual;
  61.         function  IsValid(const S: String): Boolean; virtual;
  62.       end;
  63.  
  64.     (* Validate file name *)
  65.     PFileValidator = ^TFileValidator;
  66.     TFileValidator =
  67.       object (TPathValidator)
  68.         BadName : Boolean;
  69.         procedure Error; virtual;
  70.         function  IsValid(const S: String): Boolean; virtual;
  71.       end;
  72.  
  73.     (* Validate real number *)
  74.     PRealValidator = ^TRealValidator;
  75.     TRealValidator = object(TFilterValidator)
  76.       Min, Max: Real;
  77.       Width, Decimals : Integer;
  78.       constructor Init(AMin, AMax: Real);
  79.       constructor Load(var S: TStream);
  80.       procedure Error; virtual;
  81.       function  IsValid(const S: String): Boolean; virtual;
  82.       procedure Store(var S: TStream);
  83.       function  Transfer(var S:String; Buffer:Pointer; Flag:TVTransfer):Word; virtual;
  84.     end;
  85.  
  86.     (* Validate hex number, four hex digits *)
  87.     PHexValidator = ^THexValidator;
  88.     THexValidator = object(TFilterValidator)
  89.       Min, Max: Word;
  90.       constructor Init(AMin, AMax:Word);
  91.       constructor Load(var S: TStream);
  92.       procedure Error; virtual;
  93.       function  IsValid(const S: String): Boolean; virtual;
  94.       procedure Store(var S: TStream);
  95.       function  Transfer(var S:String; Buffer:Pointer; Flag:TVTransfer):Word; virtual;
  96.     end;
  97.  
  98.     (* A Longint validator that updates a scrollbar *)
  99.     PSliderValidator = ^TSliderValidator;
  100.     TSliderValidator =
  101.       object (TRangeValidator)
  102.         Slider : PScrollbar;
  103.         constructor Init(AMin, AMax:Longint; ASlider:PScrollbar);
  104.         function IsValidInput(var S:String; SuppressFill:Boolean):Boolean; virtual;
  105.       end;
  106.  
  107.  
  108.   const
  109.     (* Help contexts for Borland's ColorSel dialog *)
  110.     ColorSelHelpCtxList : array [1..7] of Word =
  111.       (hcCancel, hcOK, hctoyCSMonoSelector,
  112.        hctoyCSBackground, hctoyCSForeground,
  113.        hctoyCSItem, hctoyCSGroup);
  114.  
  115.   var
  116.     (* True if the Validator is updating the slider, rather than vice versa *)
  117.     IgnoreSliderMessage : Boolean;
  118.  
  119.  
  120.   (* Display a notice in a box *)
  121.   procedure Notice(const Title, Text:String);
  122.   procedure NoNotice;
  123.  
  124.   (* Add help contexts to existing dialogs without builtin contexts *)
  125.   procedure AddHelpCtx(P:PGroup; HelpCtxList:PWord);
  126.  
  127.   procedure DisposeMenuItems(Items:PMenuItem);
  128.   function  StorePointer(var Save; Ref:Pointer):Pointer;
  129.  
  130.  
  131. (***************************************************************************
  132. ***************************************************************************)
  133. implementation
  134.  
  135.   procedure TPathValidator.Error;
  136.   begin
  137.     MessageBox(^M^C'Invalid path', Nil, mfError+mfOkButton);
  138.   end;
  139.  
  140.  
  141.   (*******************************************************************
  142.     Try to validate a path
  143.   *******************************************************************)
  144.   function TPathValidator.IsValid;
  145.     var
  146.       SR : SearchRec;
  147.   begin
  148.     FindFirst(AddBackslash(S)+'*.*', AnyFile, SR);
  149.     IsValid:=DosError<>3;
  150.   end;
  151.  
  152.  
  153.     (*******************************************************************
  154.     *******************************************************************)
  155.  
  156.   procedure TFileValidator.Error;
  157.   begin
  158.     if BadName then
  159.       MessageBox(^M^C'Invalid file name', Nil, mfError+mfOkButton)
  160.     else
  161.       inherited Error;
  162.   end;
  163.  
  164.  
  165.   (*******************************************************************
  166.     Try to see if it is a valid file name, difficult and not
  167.     quite reliable
  168.   *******************************************************************)
  169.   function TFileValidator.IsValid;
  170.     var
  171.       SR : SearchRec;
  172.   begin
  173.     BadName:=False;
  174.     FindFirst(S, AnyFile-Directory, SR);
  175.     if (DosError=18) or (S[Length(S)]='\') then
  176.     begin
  177.       BadName:=True;
  178.       FindFirst(S+'\*.*', AnyFile, SR);
  179.       IsValid:=DosError=3;
  180.     end
  181.     else
  182.       IsValid:=DosError<>3;
  183.   end;
  184.  
  185.  
  186.     (*******************************************************************
  187.     *******************************************************************)
  188.  
  189.   (*******************************************************************
  190.     Simple real validator
  191.   *******************************************************************)
  192.   constructor TRealValidator.Init;
  193.   begin
  194.     inherited Init(['0'..'9','+','-','.']);
  195.     if AMin >= 0 then ValidChars:=ValidChars - ['-'];
  196.     Min:=AMin;
  197.     Max:=AMax;
  198.   end;
  199.  
  200.   constructor TRealValidator.Load(var S: TStream);
  201.   begin
  202.     inherited Load(S);
  203.     S.Read(Min, SizeOf(Max) + SizeOf(Min));
  204.   end;
  205.  
  206.   procedure TRealValidator.Error;
  207.   var
  208.     Params: array [0..1] of Longint;
  209.   begin
  210.     Params[0]:=Round(Min);
  211.     Params[1]:=Round(Max);
  212.     MessageBox('Value not in the range %d to %d', @Params,
  213.       mfError + mfOKButton);
  214.   end;
  215.  
  216.   function TRealValidator.IsValid(const S: String): Boolean;
  217.   var
  218.     Value: Real;
  219.     Code: Integer;
  220.   begin
  221.     IsValid:=False;
  222.     if inherited IsValid(S) then
  223.     begin
  224.       Val(S, Value, Code);
  225.       if (Code = 0) and (Value >= Min) and (Value <= Max) then
  226.         IsValid:=True;
  227.     end;
  228.   end;
  229.  
  230.   procedure TRealValidator.Store(var S: TStream);
  231.   begin
  232.     inherited Store(S);
  233.     S.Write(Min, SizeOf(Max) + SizeOf(Min));
  234.   end;
  235.  
  236.  
  237.   (*******************************************************************
  238.     Transfer a real
  239.   *******************************************************************)
  240.   function TRealValidator.Transfer(var S: String; Buffer: Pointer; Flag: TVTransfer): Word;
  241.   var
  242.     Value: Real;
  243.     Code: Integer;
  244.   begin
  245.     if Options and voTransfer <> 0 then
  246.     begin
  247.       Transfer:=SizeOf(Value);
  248.       case Flag of
  249.        vtGetData:
  250.          begin
  251.            Val(S, Value, Code);
  252.            Real(Buffer^):=Value;
  253.          end;
  254.        vtSetData:
  255.          Str(Real(Buffer^):Width:Decimals, S);
  256.       end;
  257.     end
  258.     else
  259.       Transfer:=0;
  260.   end;
  261.  
  262.  
  263.     (*******************************************************************
  264.     *******************************************************************)
  265.  
  266.   (*******************************************************************
  267.     Simple hex validator, four hex digits
  268.   *******************************************************************)
  269.   constructor THexValidator.Init;
  270.   begin
  271.     inherited Init(['0'..'9','A'..'F','a'..'f']);
  272.     Options:=Options or voTransfer;
  273.     Min:=AMin;
  274.     Max:=AMax;
  275.   end;
  276.  
  277.   constructor THexValidator.Load(var S: TStream);
  278.   begin
  279.     inherited Load(S);
  280.     S.Read(Min, SizeOf(Max) + SizeOf(Min));
  281.   end;
  282.  
  283.   procedure THexValidator.Error;
  284.   var
  285.     Params: array [0..1] of Longint;
  286.   begin
  287.     Params[0]:=Min;
  288.     Params[1]:=Max;
  289.     MessageBox('Value not in the range %d to %d', @Params,
  290.       mfError + mfOKButton);
  291.   end;
  292.  
  293.   function THexValidator.IsValid(const S: String): Boolean;
  294.     var
  295.       Value: Real;
  296.   begin
  297.     IsValid:=False;
  298.     if inherited IsValid(S) and (S<>'') then
  299.     begin
  300.       Value:=HexStrValue(S);
  301.       IsValid:=(Value >= Min) and (Value <= Max);
  302.     end;
  303.   end;
  304.  
  305.   procedure THexValidator.Store(var S: TStream);
  306.   begin
  307.     inherited Store(S);
  308.     S.Write(Min, SizeOf(Max) + SizeOf(Min));
  309.   end;
  310.  
  311.  
  312.   (*******************************************************************
  313.     Transfer a hex Word
  314.   *******************************************************************)
  315.   function THexValidator.Transfer(var S:String; Buffer:Pointer; Flag:TVTransfer):Word;
  316.   begin
  317.     if Options and voTransfer <> 0 then
  318.     begin
  319.       Transfer:=SizeOf(Word);
  320.       case Flag of
  321.        vtGetData:  Word(Buffer^):=HexStrValue(S);
  322.        vtSetData:  S:=HexStr(Word(Buffer^));
  323.       end;
  324.     end
  325.     else
  326.       Transfer:=0;
  327.   end;
  328.  
  329.  
  330.     (*******************************************************************
  331.     *******************************************************************)
  332.  
  333.   (*******************************************************************
  334.     Slider init
  335.   *******************************************************************)
  336.   constructor TSliderValidator.Init;
  337.   begin
  338.     inherited Init(AMin, AMax);
  339.     Slider:=ASlider;
  340.   end;
  341.  
  342.  
  343.   (*******************************************************************
  344.     Update the slider when the input line changes
  345.   *******************************************************************)
  346.   function TSliderValidator.IsValidInput;
  347.     var
  348.       n : Longint;
  349.   begin
  350.     IsValidInput:=inherited IsValidInput(S, SuppressFill);
  351.  
  352.     if Transfer(S, @n, vtGetData)>0 then
  353.     begin
  354.       if n>Max then
  355.         n:=Max
  356.       else
  357.         if n<Min then
  358.           n:=Min;
  359.  
  360.       IgnoreSliderMessage:=True;
  361.       Slider^.SetValue(n);
  362.       IgnoreSliderMessage:=False;
  363.     end;
  364.   end;
  365.  
  366.  
  367. (***************************************************************************
  368. ***************************************************************************)
  369.  
  370.   var
  371.     NoticeBox : PDialog;
  372.  
  373.   (*******************************************************************
  374.     Post a notice on screen
  375.   *******************************************************************)
  376.   procedure Notice(const Title, Text:String);
  377.     var
  378.       R : TRect;
  379.   begin
  380.     R.Assign(0, 0, 14+Length(Text), 7);
  381.     New(NoticeBox, Init(R, Title));
  382.     R.Grow(-1,-1);
  383.     NoticeBox^.Insert(New(PStaticText, Init(R, Text)));
  384.     NoticeBox^.Options:=NoticeBox^.Options or ofCentered;
  385.     NoticeBox^.Flags:=0;
  386.     Application^.InsertWindow(NoticeBox);
  387.   end;
  388.  
  389.  
  390.   (*******************************************************************
  391.     Remove the notice box
  392.   *******************************************************************)
  393.   procedure NoNotice;
  394.   begin
  395.     if NoticeBox<>Nil then
  396.     begin
  397.       Dispose(NoticeBox, Done);
  398.       NoticeBox:=Nil;
  399.     end;
  400.   end;
  401.  
  402.  
  403. (***************************************************************************
  404. ***************************************************************************)
  405.  
  406.   (*******************************************************************
  407.     Add help contexts to a dialog
  408.   *******************************************************************)
  409.   procedure AddHelpCtx(P:PGroup; HelpCtxList:PWord);
  410.     procedure Addhc(P:PView); far;
  411.     begin
  412.       if (P^.Options and ofSelectable)<>0 then
  413.       begin
  414.         P^.HelpCtx:=HelpCtxList^;
  415.         Inc(HelpCtxList);
  416.       end;
  417.     end;
  418.   begin
  419.     P^.ForEach(@Addhc);
  420.   end;
  421.  
  422.  
  423.   (*******************************************************************
  424.     Disposes of a linked list of Menu items
  425.   *******************************************************************)
  426.   procedure DisposeMenuItems(Items:PMenuItem);
  427.   begin
  428.     DisposeMenu(NewMenu(Items));
  429.   end;
  430.  
  431.  
  432.   (*******************************************************************
  433.     Filter that saves a pointer
  434.     Useful for catching a specific menu item during menu construction
  435.   *******************************************************************)
  436.   function StorePointer(var Save; Ref:Pointer):Pointer;
  437.   begin
  438.     Pointer(Save):=Ref;
  439.     StorePointer:=Ref;
  440.   end;
  441.  
  442.  
  443. end.